//
//  HBAlertView.m
//  HBActionSheet
//
//  Created by 韩冰 on 2017/10/23.
//  Copyright © 2017年 韩冰. All rights reserved.
//

#import "HBAlertView.h"
#import "HBCommon.h"

@interface HBAlertView ()

@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *detailLabel;
@property (nonatomic, strong)UIView *topLine;
@property (nonatomic, strong)UIView *middleLine;
@property (nonatomic, strong)UIView *lastLine;

@end

@implementation HBAlertView

- (instancetype)initWithTitle:(NSString *)title
                       detail:(NSString *)detail
                        items:(NSArray *)items
                      handler:(void (^)(NSInteger index))handerAction{
    
    self = [super init];
    if (self) {
        NSAssert(items.count>0, @"Could not find any items.");
        [self defaulSet];
        self.touchBgWildToHide = YES;
        self.type = HBPopViewTypeAlert;
        self.layer.cornerRadius = self.cornerRadius;
        self.clipsToBounds = YES;
        self.layer.borderWidth = MM_SPLIT_WIDTH;
        self.layer.borderColor = self.lineColor.CGColor;
        self.width = self.viewWidth?self.viewWidth:256;
        self.title = title;
        self.detail = detail;
        if (self.title) {
            self.titleLabel.frame = CGRectMake(0, 0, self.width, self.titleHeight);
            self.titleLabel.text = self.title;
            [self addSubview:self.titleLabel];
            self.topLine.frame = CGRectMake(0, self.titleLabel.bottom, self.width, self.lineHeight);
            [self addSubview:self.topLine];
        }
        if (self.detail) {
            self.detailLabel.frame =CGRectMake(self.innerMargin, self.topLine.bottom + self.innerMargin, self.width - (2*self.innerMargin), 50);
            [self.detailLabel setAttributedText:[[[NSMutableAttributedString alloc]initWithString:self.detail] stringWithParagraph]];;
            self.detailLabel.numberOfLines = 0;
            [self.detailLabel sizeToFit];
            self.detailLabel.frame = CGRectMake(self.innerMargin, self.topLine.bottom + self.innerMargin, self.width - (2*self.innerMargin), self.detailLabel.height +10);
            [self addSubview:self.detailLabel];
        }
        self.middleLine.frame = CGRectMake(0,  ceil(self.detailLabel.bottom + self.innerMargin), self.width, self.lineHeight);
        [self addSubview:self.middleLine];
        if (items.count == 1) {
           UIButton *button =  [self creatButtonItem];
            [button setTitleColor:self.itemHighlightColor forState:UIControlStateNormal];
            [button setTitle:[items objectAtIndexSafely:0] forState:UIControlStateNormal];
            button.frame = CGRectMake(0, self.middleLine.bottom, self.width, self.buttonHeight);
            button.tag = 20000;
        }else if (items.count ==2){
            UIButton *canclebutton =  [self creatButtonItem];
            [canclebutton setTitleColor:self.itemHighlightColor forState:UIControlStateNormal];
            [canclebutton setTitle:[items objectAtIndexSafely:1] forState:UIControlStateNormal];
            canclebutton.frame = CGRectMake(0, self.middleLine.bottom, self.width/2.0, self.buttonHeight);
            canclebutton.tag = 20000;
            self.lastLine.frame = CGRectMake(ceil(self.width/2.0 - self.lineHeight), 0, self.lineHeight, self.buttonHeight);
            [canclebutton addSubview:self.lastLine];
            
            UIButton *button =  [self creatButtonItem];
            [button setTitleColor:self.itemNormalColor forState:UIControlStateNormal];
            [button setTitle:[items objectAtIndexSafely:0] forState:UIControlStateNormal];
            button.frame = CGRectMake(self.width/2.0, self.middleLine.bottom, self.width/2.0, self.buttonHeight);
            button.tag = 20001;
       
        }else {
            if (items.count > 2) {
                CGFloat Y = self.middleLine.bottom ;
                for (int i = 0; i < items.count; i ++) {
                    UIButton *button =  [self creatButtonItem];
                  [button setTitle:[items objectAtIndexSafely:i] forState:UIControlStateNormal];
                    if (i == items.count -1) {
                        [button setTitleColor:self.itemHighlightColor forState:UIControlStateNormal];
                        button.tag = 20000;
                          button.frame = CGRectMake(0, Y, self.width, self.buttonHeight);
                    }else{
                        [button setTitleColor:self.itemNormalColor forState:UIControlStateNormal];
                        button.tag = 20001 + i;
                        button.frame = CGRectMake(0, Y, self.width, self.buttonHeight);
                        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, ceil(button.height - self.lineHeight), self.width, self.lineHeight)];
                        line.backgroundColor = self.lineColor;
                        [button addSubview:line];
                    }
                   
                    Y = Y + self.buttonHeight;
                }
            }
        }
        CGFloat H = 0;
        if (items.count <= 2) {
            H = self.buttonHeight;
        }else{
            
            H = self.buttonHeight *items.count;
        }
        self.frame = CGRectMake(0, 0, self.width, self.middleLine.bottom + H);
        self.itemHandle = ^(NSInteger index){
            handerAction(index);
        };
       
    }
    return self;
}
- (void)defaulSet{
    self.backgroundColor = [UIColor redColor];
    self.titleHeight = 30;
    self.titleFontSize = 12;
    self.titleColor = [UIColor HB_ColorWithHexString:@"#DEDEDE"];
    self.lineColor = [UIColor HB_ColorWithHexString:@"#E1E1E1"];
    self.detailColor =[UIColor HB_ColorWithHexString:@"#000000"];
    self.detailFontSize = 14;
    self.buttonFontSize = 14;
    self.itemNormalColor = [UIColor blackColor];
    self.itemHighlightColor = [UIColor blueColor];
    self.buttonHeight = 40;
    self.cornerRadius = 5.0;
    self.layer.backgroundColor = [[UIColor whiteColor] CGColor];
    self.innerMargin = 10;
    self.lineHeight = 0.5;
    
}
- (UIButton *)creatButtonItem{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:self.buttonFontSize];
    [button addTarget:self action:@selector(itemAction:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:button];
    return button;
}

- (void)itemAction:(UIButton *)btn{
    if ((btn.tag -20000)== 0)
    {
        [self hide];
    }else{
        [self hide];
    }
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = [UIFont systemFontOfSize:self.titleFontSize];
        _titleLabel.textColor = self.titleColor;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UILabel *)detailLabel{
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _detailLabel.font = [UIFont systemFontOfSize:self.detailFontSize];
        _detailLabel.textColor = self.detailColor;
        _detailLabel.textAlignment = NSTextAlignmentCenter;
        [_detailLabel sizeToFit];
    }
    return _detailLabel;
}

- (UIView  *)topLine{
    if (!_topLine) {
        _topLine = [[UIView alloc]initWithFrame:CGRectZero];
        _topLine.backgroundColor = self.lineColor;
    }
    return _topLine;
}
- (UIView  *)middleLine{
    if (!_middleLine) {
        _middleLine = [[UIView alloc]initWithFrame:CGRectZero];
        _middleLine.backgroundColor = self.lineColor;
    }
    return _middleLine;
}
- (UIView *)lastLine{
    if (!_lastLine) {
        _lastLine = [[UIView alloc]initWithFrame:CGRectZero];
        _lastLine.backgroundColor = self.lineColor;
    }
    return _lastLine;
}
@end
